using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SMSSender.Entities.Models;
using SMSSender.Entities.Utils;
using SMSSender.Common;

namespace SMSSender.DAL.Base
{
	public class CustomerHistoryDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CustomerHistoryDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CustomerHistory table.
		/// </summary>
		public virtual void Insert(CustomerHistoryInfo customerHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdAo", customerHistoryInfo.IdAo),
				new SqlParameter("@Action", customerHistoryInfo.Action),
				new SqlParameter("@Postdate", customerHistoryInfo.Postdate),
				new SqlParameter("@IdPhone", customerHistoryInfo.IdPhone),
				new SqlParameter("@PhoneNumber", customerHistoryInfo.PhoneNumber),
				new SqlParameter("@NameCustomer", customerHistoryInfo.NameCustomer),
				new SqlParameter("@Address", customerHistoryInfo.Address)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerHistory_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CustomerHistory table.
		/// </summary>
		public virtual void Update(CustomerHistoryInfo customerHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdAo", customerHistoryInfo.IdAo),
				new SqlParameter("@Action", customerHistoryInfo.Action),
				new SqlParameter("@Postdate", customerHistoryInfo.Postdate),
				new SqlParameter("@IdPhone", customerHistoryInfo.IdPhone),
				new SqlParameter("@PhoneNumber", customerHistoryInfo.PhoneNumber),
				new SqlParameter("@NameCustomer", customerHistoryInfo.NameCustomer),
				new SqlParameter("@Address", customerHistoryInfo.Address)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerHistory_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the CustomerHistory table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idAo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdAo", idAo)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerHistory_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CustomerHistory table.
		/// </summary>
		public virtual CustomerHistoryInfo Select(Guid? idAo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdAo", idAo)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CustomerHistory_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCustomerHistoryInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the CustomerHistory table.
		/// </summary>
		public List<CustomerHistoryInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CustomerHistory_SelectAll"))
			{
				List<CustomerHistoryInfo> customerHistoryInfoList = new List<CustomerHistoryInfo>();
				while (dataReader.Read())
				{
					customerHistoryInfoList.Add(MakeCustomerHistoryInfo(dataReader));
				}

				return customerHistoryInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the CustomerHistory class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CustomerHistoryInfo MakeCustomerHistoryInfo(SqlDataReader dataReader)
		{
			CustomerHistoryInfo customerHistoryInfo = new CustomerHistoryInfo();
			customerHistoryInfo.IdAo = SqlClientUtility.GetGuid(dataReader, DbConstants.CUSTOMERHISTORY.IDAO);
			customerHistoryInfo.Action = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERHISTORY.ACTION);
			customerHistoryInfo.Postdate = SqlClientUtility.GetDecimal(dataReader, DbConstants.CUSTOMERHISTORY.POSTDATE);
			customerHistoryInfo.IdPhone = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERHISTORY.IDPHONE);
			customerHistoryInfo.PhoneNumber = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERHISTORY.PHONENUMBER);
			customerHistoryInfo.NameCustomer = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERHISTORY.NAMECUSTOMER);
			customerHistoryInfo.Address = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERHISTORY.ADDRESS);

			return customerHistoryInfo;
		}

		#endregion
	}
}
